package com.app.housemanage.controller;

import com.app.housemanage.domain.Properties;
import com.app.housemanage.domain.dto.PropertyQueryDTO;
import com.app.housemanage.domain.vo.ApiResult;
import com.app.housemanage.domain.vo.PageResult;
import com.app.housemanage.domain.vo.PropertiyVo;
import com.app.housemanage.domain.vo.ResponseUtils;
import com.app.housemanage.service.PropertiesService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 房产管理控制器
 */
@RestController
@RequestMapping("/api/properties")
public class PropertiesController {

    @Autowired
    private PropertiesService propertiesService;

    /**
     * 获取所有房产列表
     */
    @GetMapping
    public ResponseEntity<List<Properties>> getAllProperties() {
        List<Properties> properties = propertiesService.list();
        return ResponseEntity.ok(properties);
    }

    /**
     * 根据ID查询房产信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<Properties> getPropertyById(@PathVariable Integer id) {
        Properties property = propertiesService.getById(id);
        if (property != null) {
            return ResponseEntity.ok(property);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 根据楼栋号查询房产列表
     */
    @GetMapping("/buildings")
    public ApiResult<List<String>> getPropertiesByBuilding() {
        List<String> properties = propertiesService.getPropertiesByBuilding();
        return ApiResult.success(properties);
    }

    /**
     * 根据房产状态查询房产列表
     */
    @GetMapping("/status/{status}")
    public ResponseEntity<List<Properties>> getPropertiesByStatus(@PathVariable String status) {
        List<Properties> properties = propertiesService.getPropertiesByStatus(status);
        return ResponseEntity.ok(properties);
    }

    /**
     * 根据业主ID查询房产列表
     */
    @GetMapping("/owner/{ownerId}")
    public ResponseEntity<List<Properties>> getPropertiesByOwnerId(@PathVariable Integer ownerId) {
        List<Properties> properties = propertiesService.getPropertiesByOwnerId(ownerId);
        return ResponseEntity.ok(properties);
    }

    /**
     * 多条件查询房产列表
     */
    @GetMapping("/search")
    public ApiResult<PageResult<PropertiyVo>> searchProperties(
            PropertyQueryDTO propertyQueryDTO) {
        List<PropertiyVo> properties = propertiesService.searchProperties(propertyQueryDTO);
        PageResult<PropertiyVo> pageResult = PageResult.of(properties);
        return ApiResult.success(pageResult);
    }

    /**
     * 创建新房产
     */
    @PostMapping
    public ResponseEntity<Properties> createProperty(@RequestBody Properties property) {
        boolean saved = propertiesService.save(property);
        if (saved) {
            return ResponseEntity.ok(property);
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    /**
     * 更新房产信息
     */
    @PutMapping("/{id}")
    public ResponseEntity<Properties> updateProperty(@PathVariable Integer id, @RequestBody Properties property) {
        property.setId(id);
        boolean updated = propertiesService.updateById(property);
        if (updated) {
            return ResponseEntity.ok(property);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 删除房产信息
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteProperty(@PathVariable Integer id) {
        boolean deleted = propertiesService.removeById(id);
        if (deleted) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}